package com.ssps.back.platform.service.Impl;

import com.ssps.back.platform.service.UserInfoService;
import com.ssps.entity.UserInfo;
import com.ssps.entity.UserInfoExample;
import com.ssps.mapper.UserInfoMapper;
import com.ssps.utils.Idcard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author li
 * @time 2020/07/22 19:30:23
 */

@Service
public class UserInfoServiceImpl  implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> findAll(String status, String uname) {

        UserInfoExample example=new UserInfoExample();
        UserInfoExample.Criteria criteria = example.createCriteria();
        if (status!=null&&status!=""){
            criteria.andStatusEqualTo(status);
        }
        if (uname!=null&&uname!=""){
            criteria.andUnameLike("%"+uname+"%");
        }
        List<UserInfo> userInfoList=userInfoMapper.selectByExample(example);
        return userInfoList;
    }


    /**
     *
     * 点击审核的时候
     * 会将信息id传到后台
     * 然后通过id找到图片
     * 然后读出信息
     * 通过验证身份证的长度是否等于18位
     * 来将数据写入表中
     *
     */

    @Override
    public void userInfoSave(Integer uiid) {
        UserInfo userInfo=new UserInfo();
        userInfo=userInfoMapper.selectByPrimaryKey(uiid);
        String idcard = Idcard.idcard (userInfo.getIdCardBack());

        System.out.println("idcard = " + idcard);
        String[] keys = "  住址 出生 姓名 公民身份号码 ".split(" ");
        Map<String, String> idcardMap = new HashMap<String, String>();
        for (String key : keys) {
            Pattern p = Pattern.compile(key + ".*?\"words\": \"(.*?)\"");
            Matcher matcher = p.matcher(idcard);
            if (matcher.find()) {
                String value = matcher.group(1);
                //System.out.println(key + " = " + value);
                idcardMap.put(key, value);
            }
        }
        Date date=new Date();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        try {
            String str=idcardMap.get("出生");
            StringBuilder sb = new StringBuilder(str);
            sb.insert(4,"-");
            sb.insert(7,"-");
            date=df.parse(sb.toString());
            //指定日期/时间解析是否不严格，默认是不严格(true)
            df.setLenient(false);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String idCard=idcardMap.get("公民身份号码");
        if (idCard.length()==18){

        userInfo.setUname(idcardMap.get("姓名"));
        userInfo.setBirthday(date);

        String address=idcardMap.get("住址");

         StringBuffer strBuf = new StringBuffer(address);

            int shengIndex = strBuf.indexOf("省");
            if (shengIndex!=-1) {
                strBuf.insert((shengIndex + 1), ",");
            }
            int shiIndex = strBuf.indexOf("市");
            if (shiIndex!=-1) {
                strBuf.insert((shiIndex + 1), ",");
            }
            int qvindex = strBuf.indexOf("区");
            if (qvindex!=-1){
                strBuf.insert((qvindex + 1), ",");
            }
            int zhenindex = strBuf.indexOf("镇");

            if (zhenindex!=-1) {
                strBuf.insert((zhenindex + 1), ",");
            }
        userInfo.setAddress(strBuf.toString());
        userInfo.setIdCard(idCard);
        userInfo.setStatus("审核通过");

        userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }


    }

    @Override
    public void userInfoUpdate(UserInfo userInfo) {
        userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public UserInfo finOne(Integer uid) {
        UserInfoExample example=new UserInfoExample();
        UserInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        List<UserInfo>userInfoList=userInfoMapper.selectByExample(example);


        return userInfoList.isEmpty() ? null:userInfoList.get(0);
    }


}
